package com.app.buss.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.app.buss.R;
import com.app.buss.base.BaseActivity;
import com.app.buss.bean.WebBean;
import com.app.buss.mvp.presenter.MainPresenter;
import com.app.buss.mvp.view.IMainView;
import com.app.buss.utils.StatusBarUtil;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Forweb3Activity extends BaseActivity<MainPresenter> implements IMainView {
    @BindView(R.id.ll_web)
    WebView llWeb;
    @BindView(R.id.app_tool)
    LinearLayout appTool;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private String url = "https://www.google.com";
    private String title;
    private String json;
    private boolean isCanBack;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected int bindContent() {
        return R.layout.aty_forweb;
    }

    @Override
    protected void initSome() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            json = getIntent().getExtras().getString("data");
            Gson gson = new Gson();
            WebBean bean = gson.fromJson(json, WebBean.class);
            if ((bean.getHasTitleBar() instanceof Boolean && (Boolean) bean.getHasTitleBar())
                    || (bean.getHasTitleBar() instanceof String && bean.getHasTitleBar().equals("true"))) {
                appTool.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(bean.getTitleTextColor()))
                    toolbarTitle.setTextColor(Color.parseColor(bean.getTitleTextColor()));
                if (!TextUtils.isEmpty(bean.getTitleColor()))
                    toolbar.setBackgroundColor(Color.parseColor(bean.getTitleColor()));
                if (bean.getTitle() != null)
                    initToolBar(bean.getTitle());
                if (!TextUtils.isEmpty(bean.getStateBarTextColor())) {
                    if (bean.getStateBarTextColor().equals("black")) {
                        if (!TextUtils.isEmpty(bean.getTitleColor()))
                            StatusBarUtil.setStatusBarMode(Forweb3Activity.this, true, Color.parseColor(bean.getTitleColor()));
                        else
                            StatusBarUtil.setStatusBarMode(Forweb3Activity.this, true, Color.parseColor("#DDBA7A"));
                    } else {
                        if (!TextUtils.isEmpty(bean.getTitleColor()))
                            StatusBarUtil.setStatusBarMode(Forweb3Activity.this, false, Color.parseColor(bean.getTitleColor()));
                        else
                            StatusBarUtil.setStatusBarMode(Forweb3Activity.this, false, Color.parseColor("#DDBA7A"));
                    }
                }
                if ((bean.getWebBack() instanceof Boolean && (Boolean) bean.getWebBack())
                        || (bean.getWebBack() instanceof String && bean.getWebBack().equals("true"))) {
                    isCanBack = true;
                } else {
                    isCanBack = false;
                }
                url = bean.getUrl();
            } else {
                appTool.setVisibility(View.GONE);
            }
        }
        llWeb.loadUrl(url);
    }
    @Override
    public void onBackPressed() {
            if(isCanBack) {
                if (!llWeb.canGoBack())
                    super.onBackPressed();
                else
                    llWeb.goBack();
            }else {
                finish();
            }
    }


    @Override
    public void getDataSuccess(Object model) {

    }

    @Override
    public void getDataFail(String msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
